//
//  MCVCollectionViewController.m
//  QuKeClient
//
//  Created by delicious on 2018/1/13.
//  Copyright © 2018年 delicious. All rights reserved.
//

#import "MCVCollectionViewController.h"



@implementation MCVCollectionViewController

- (instancetype)initWithCollectionViewLayout:(UICollectionViewLayout *)layout
{
    self = [super init];
    if(self)
    {
        self.startIndex=self.currentPageIndex = 1;
        BaseCollectionView *cV = [[BaseCollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        cV.backgroundColor = [UIColor whiteColor];
        cV.delegate = self;
        cV.dataSource = self;
        self.collectionView = (id)cV;
    }
    return self;
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.view addSubview:self.collectionView];
    self.dataArr = [NSMutableArray array];
  
}


#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataArr.count;
}

- (BaseCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MCCollVCellInfo *info = [self collectionView:collectionView layout:collectionView.collectionViewLayout cellForIndexPath:indexPath sectionAtIndex:indexPath.section IsSection:YES];
    
    BaseCollectionViewCell *cell = (id)[BaseCollectionViewCell onCollectionView:collectionView forViewController:self dequeueReusableCellWithReuseIdentifier:info.cellId dataSource:[self cellInitForSourceAtIndexPath:indexPath] forIndexPath:indexPath];
    if (NO==cell.isAsInt) {
        [self onSetSubviewSELForCell:cell forCollectionView:collectionView forIndexPath:indexPath];
        cell.isAsInt =YES;
    }
    
    
    return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    MCCollVCellInfo *info = [self collectionView:collectionView layout:collectionViewLayout cellForIndexPath:nil sectionAtIndex:section IsSection:YES];
    return info.insetsCell;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    MCCollVCellInfo *info = [self collectionView:collectionView layout:collectionViewLayout cellForIndexPath:nil sectionAtIndex:section IsSection:YES];
    return info.lineSpacing;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    MCCollVCellInfo *info = [self collectionView:collectionView layout:collectionViewLayout cellForIndexPath:nil sectionAtIndex:section IsSection:YES];
    return info.interitemSpacing;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    MCCollVCellInfo *info = [self collectionView:collectionView layout:collectionViewLayout cellForIndexPath:indexPath sectionAtIndex:indexPath.section IsSection:NO];
    return info.sizeCell;
}



- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self onUpdateCell:cell model:[self modelForIndexPath:indexPath] atIndexPath:indexPath];
}



-(BaseCollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    NSString *UICollectionElementKin;
    MCReusableViewInfo * viewInfo;
    id model;
    if (kind == UICollectionElementKindSectionHeader){
        viewInfo = [self collectionView:collectionView HeaderSectionAtIndex:indexPath.section AtIndexPath:indexPath IsSize:NO];
        if (!viewInfo) {
            return nil;
        }
        UICollectionElementKin = UICollectionElementKindSectionHeader;
        model = [self reusableHeadterModelForIndexPath:indexPath];
        
    }
    else
    {
        viewInfo = [self collectionView:collectionView FooderSectionAtIndex:indexPath.section AtIndexPath:indexPath IsSize:NO];
        if (!viewInfo) {
            return nil;
        }
        model = [self reusableFoodterModelForIndexPath:indexPath];
        UICollectionElementKin = UICollectionElementKindSectionFooter;
    }
    
    BaseCollectionReusableView *reusableView = [BaseCollectionReusableView onCollectionView:collectionView forViewController:self reuseIdentifier:viewInfo.viewId forforKind:UICollectionElementKin forIndexPath:indexPath];
    if (reusableView.isAsInt ==NO) {
        if (UICollectionElementKin == UICollectionElementKindSectionFooter) {
            [self onSetSubviewSELForFooderView:reusableView forCollectionView:collectionView forIndexPath:indexPath];
        }else
        {
            [self onSetSubviewSELForHeaderView:reusableView forCollectionView:collectionView forIndexPath:indexPath];
        }
        reusableView.isAsInt= YES;
    }
    
    
    [reusableView update:model forCollectionView:collectionView AtIndexPath:indexPath];
    return reusableView;
}
//- (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
//{
//    if (elementKind == UICollectionElementKindSectionHeader){
//        viewInfo = [self collectionView:collectionView HeaderSectionAtIndex:indexPath.section AtIndexPath:indexPath IsSize:NO];
//        if (!viewInfo) {
//            return nil;
//        }
//        UICollectionElementKin = UICollectionElementKindSectionHeader;
//        model = [self reusableHeadterModelForIndexPath:indexPath];
//
//    }
//    else
//    {
//        viewInfo = [self collectionView:collectionView FooderSectionAtIndex:indexPath.section AtIndexPath:indexPath IsSize:NO];
//        if (!viewInfo) {
//            return nil;
//        }
//        model = [self reusableFoodterModelForIndexPath:indexPath];
//        UICollectionElementKin = UICollectionElementKindSectionFooter;
//    }
//
//}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    MCReusableViewInfo * ViewInfo = [self collectionView:collectionView HeaderSectionAtIndex:section AtIndexPath:nil IsSize:YES];
    return ViewInfo.sizeView;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    MCReusableViewInfo * ViewInfo = [self collectionView:collectionView FooderSectionAtIndex:section AtIndexPath:nil IsSize:YES];
    return ViewInfo.sizeView;
}

@end





